/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.agent.toolbox.aspect;

import com.alibaba.fastjson.JSONObject;
import com.hyts.agent.base.logger.AgentLogger;
import com.hyts.agent.base.param.AgentTransferParam;
import com.hyts.agent.filter.AgentAspectInterceptor;
import com.hyts.agent.filter.MethodInterceptor;
import com.hyts.agent.logger.AgentLogManager;
import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;

import java.lang.reflect.Method;
import java.text.MessageFormat;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.agent.sample
 * @author:LiBo/Alex
 * @create-date:2020-04-29 14:04
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class ScheduleMethodInterceptor extends AgentAspectInterceptor {

    AgentLogger agentLogger = AgentLogManager.getAgentLogger(ScheduleMethodInterceptor.class);

    @Override
    protected MethodInterceptor getMethodInterceptor() {
        return new MethodInterceptor() {
            @Override
            public Object before(Object targetSubject, Object[] params, Method method, AgentTransferParam extenseObj) {
                agentLogger.info("------------执行开始------------");
                agentLogger.info(MessageFormat.format("入参：{0}", JSONObject.toJSONString(params)));
                agentLogger.info(MessageFormat.format("方法：{0}", method.getName()));
                agentLogger.info(MessageFormat.format("传输参数：{0}", extenseObj.getAgentTransferParam()));
                return null;
            }

            @Override
            public Object after(Object targetSubject, Object[] params, Method method, Object result, AgentTransferParam extenseObj) {
                agentLogger.info(MessageFormat.format("出参：{0}", JSONObject.toJSONString(result)));
                agentLogger.info(MessageFormat.format("方法：{0}", method.getName()));
                agentLogger.info(MessageFormat.format("传输参数：{0}", extenseObj.getAgentTransferParam()));
                agentLogger.info("------------执行结束------------");
                return null;
            }

            @Override
            public void throwError(Throwable throwable, AgentTransferParam extenseObj) {
                agentLogger.info("执行异常:"+throwable.getMessage());
            }
        };
    }

    @Override
    public ElementMatcher<? super NamedElement> classNameMatcher() {
        return ElementMatchers.nameStartsWith("com.hyts.schedule.DefaultJob");
    }

    @Override
    public ElementMatcher<? super MethodDescription> methodMatcher() {
        return ElementMatchers.named("execute");
    }

    @Override
    public ElementMatcher<? super MethodDescription> constructorMatcher() {
        return null;
    }
}
